<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="header-info">
						<text class="tool-icon">✨</text>
						<text class="tool-title">起名测算</text>
						<text class="tool-subtitle">起名建议和姓名测算</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">基本信息</text>
						
						<view class="form-item">
							<text class="form-label">姓氏</text>
							<input 
								v-model="params.surname" 
								placeholder="请输入姓氏"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">性别</text>
							<view class="gender-selector">
								<view 
									class="gender-item"
									:class="{ active: params.gender === 'male' }"
									@tap="selectGender('male')"
								>
									<text class="gender-icon">👨</text>
									<text class="gender-text">男孩</text>
								</view>
								<view 
									class="gender-item"
									:class="{ active: params.gender === 'female' }"
									@tap="selectGender('female')"
								>
									<text class="gender-icon">👩</text>
									<text class="gender-text">女孩</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">出生日期</text>
							<picker 
								mode="date" 
								:value="params.birthday" 
								@change="onBirthdayChange"
								class="birthday-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayBirthday }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">出生时辰</text>
							<picker 
								mode="time" 
								:value="params.birthTime" 
								@change="onBirthTimeChange"
								class="time-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayBirthTime }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">起名要求</text>
							<view class="requirement-selector">
								<view 
									v-for="req in requirements" 
									:key="req.value"
									class="requirement-item"
									:class="{ active: selectedRequirements.includes(req.value) }"
									@tap="toggleRequirement(req.value)"
								>
									<text class="requirement-text">{{ req.name }}</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">特殊要求</text>
							<textarea 
								v-model="params.specialRequests" 
								placeholder="请描述其他特殊要求或期望的寓意"
								class="request-input"
								maxlength="200"
							></textarea>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '分析中...' : '开始起名'" 
						type="primary"
						@tap="queryNaming"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="namingResult">
					<view class="result-section">
						<text class="section-title">起名分析</text>
						
						<view class="naming-analysis">
							<view class="overall-result">
								<view class="result-score">
									<view class="score-circle">
										<text class="score-text">{{ namingResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ namingResult.overall.level }}</text>
								</view>
								<view class="result-desc">
									<text class="desc-text">{{ namingResult.overall.description }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 姓名推荐 -->
				<app-card v-if="namingResult && namingResult.nameRecommendations">
					<view class="recommendations-section">
						<text class="section-title">姓名推荐</text>
						
						<view class="recommendations-list">
							<view 
								v-for="(name, index) in namingResult.nameRecommendations" 
								:key="index"
								class="recommendation-item"
							>
								<view class="name-header">
									<text class="name-text">{{ params.surname }}{{ name.name }}</text>
									<view class="name-score">
										<text class="score-text">{{ name.score }}分</text>
									</view>
								</view>
								<text class="name-meaning">{{ name.meaning }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 分析详情 -->
				<app-card v-if="namingResult && namingResult.analysis">
					<view class="analysis-section">
						<text class="section-title">分析详情</text>
						
						<view class="analysis-list">
							<view class="analysis-item">
								<text class="analysis-label">五行配置</text>
								<text class="analysis-value">{{ namingResult.analysis.fiveElements }}</text>
							</view>
							<view class="analysis-item">
								<text class="analysis-label">笔画配置</text>
								<text class="analysis-value">{{ namingResult.analysis.strokes }}</text>
							</view>
							<view class="analysis-item">
								<text class="analysis-label">音韵效果</text>
								<text class="analysis-value">{{ namingResult.analysis.phonetics }}</text>
							</view>
							<view class="analysis-item">
								<text class="analysis-label">寓意内涵</text>
								<text class="analysis-value">{{ namingResult.analysis.meaning }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 起名原则 -->
				<app-card v-if="namingResult && namingResult.namingPrinciples">
					<view class="principles-section">
						<text class="section-title">起名原则</text>
						
						<view class="principles-list">
							<view 
								v-for="(principle, index) in namingResult.namingPrinciples" 
								:key="index"
								class="principle-item"
							>
								<view class="principle-icon">
									<text class="icon-text">📋</text>
								</view>
								<text class="principle-text">{{ principle }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 建议 -->
				<app-card v-if="namingResult && namingResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">起名建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in namingResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'
import UserStateManager from '@/utils/user-state-manager.js'

export default {
	name: 'NamingToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				surname: '',
				gender: '',
				birthday: '',
				birthTime: '',
				specialRequests: ''
			},
			selectedRequirements: [],
			namingResult: null,
			loading: false,
			toolsService: null,
			requirements: [
				{ value: 'lucky', name: '寓意吉祥' },
				{ value: 'elegant', name: '典雅大方' },
				{ value: 'unique', name: '独特新颖' },
				{ value: 'traditional', name: '传统文化' },
				{ value: 'simple', name: '简洁明了' },
				{ value: 'poetic', name: '诗意优美' }
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.surname.trim().length > 0 && 
				   this.params.gender && this.params.birthday
		},
		
		displayBirthday() {
			return this.params.birthday || '选择出生日期'
		},
		
		displayBirthTime() {
			return this.params.birthTime || '选择出生时辰'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
			
			// 自动填写用户信息
			this.autoFillUserInfo()
		},
		
		/**
		 * 自动填写用户信息
		 */
		autoFillUserInfo() {
			const autoFillInfo = UserStateManager.getAutoFillInfo()
			if (autoFillInfo) {
				// 如果有用户信息，可以预填部分数据
				if (autoFillInfo.gender) {
					this.params.gender = autoFillInfo.gender
				}
				if (autoFillInfo.birthday) {
					this.params.birthday = autoFillInfo.birthday
				}
				
				uni.showToast({
					title: '已自动填入部分信息',
					icon: 'none',
					duration: 2000
				})
			} else {
				setTimeout(() => {
					UserStateManager.promptCompleteInfo()
				}, 1000)
			}
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		selectGender(gender) {
			this.params.gender = gender
		},
		
		onBirthdayChange(e) {
			this.params.birthday = e.detail.value
		},
		
		onBirthTimeChange(e) {
			this.params.birthTime = e.detail.value
		},
		
		toggleRequirement(value) {
			const index = this.selectedRequirements.indexOf(value)
			if (index > -1) {
				this.selectedRequirements.splice(index, 1)
			} else {
				this.selectedRequirements.push(value)
			}
		},
		
		async queryNaming() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善基本信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const requestParams = {
					...this.params,
					requirements: this.selectedRequirements
				}
				
				const result = await this.toolsService.getNamingInfo(requestParams)
				
				if (result.success) {
					this.namingResult = result.data
					uni.showToast({
						title: '起名完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '起名失败')
				}
			} catch (error) {
				console.error('起名测算失败:', error)
				uni.showToast({
					title: '起名失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.gender-selector {
			display: flex;
			gap: 16rpx;
			
			.gender-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #607D8B;
					background: linear-gradient(135deg, #607D8B 0%, #455A64 100%);
					
					.gender-icon,
					.gender-text {
						color: white;
					}
				}
				
				.gender-icon {
					font-size: 32rpx;
					margin-bottom: 8rpx;
				}
				
				.gender-text {
					font-size: 24rpx;
					color: #000000;
				}
			}
		}
		
		.birthday-picker,
		.time-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.requirement-selector {
			display: flex;
			flex-wrap: wrap;
			gap: 12rpx;
			
			.requirement-item {
				padding: 12rpx 20rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 20rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #607D8B;
					background-color: #607D8B;
					
					.requirement-text {
						color: white;
					}
				}
				
				.requirement-text {
					font-size: 24rpx;
					color: #000000;
				}
			}
		}
		
		.request-input {
			width: 100%;
			min-height: 120rpx;
			padding: 16rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
			line-height: 1.5;
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.naming-analysis {
		.overall-result {
			display: flex;
			align-items: center;
			padding: 32rpx;
			background: linear-gradient(135deg, #607D8B 0%, #455A64 100%);
			border-radius: 16rpx;
			
			.result-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.result-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
	}
}

.recommendations-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.recommendations-list {
		.recommendation-item {
			margin-bottom: 20rpx;
			padding: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.name-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 12rpx;
				
				.name-text {
					font-size: 32rpx;
					font-weight: bold;
					color: #607D8B;
				}
				
				.name-score {
					.score-text {
						font-size: 24rpx;
						color: #FF9800;
						font-weight: 500;
					}
				}
			}
			
			.name-meaning {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
	}
}

.analysis-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.analysis-list {
		.analysis-item {
			display: flex;
			justify-content: space-between;
			align-items: flex-start;
			margin-bottom: 16rpx;
			padding: 16rpx;
			background-color: #f8f9fa;
			border-radius: 8rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.analysis-label {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-right: 16rpx;
				min-width: 120rpx;
			}
			
			.analysis-value {
				flex: 1;
				font-size: 26rpx;
				color: #000000;
				line-height: 1.5;
			}
		}
	}
}

.principles-section,
.suggestions-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.principles-list,
	.suggestions-list {
		.principle-item,
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.principle-icon,
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.principle-text,
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
}
</style>